<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');
/**
 * 
 * 
 * @copyright       [YOUNET_COPYRIGHT]
 * @author          YouNet Company
 * @package         YouNet_Wall
 */
class Wall_Component_Block_Display extends Phpfox_Component 
{
	/**
	 * Class process method wnich is used to execute this component.
	 */
	public function process()
	{
        // Additional params
        $iLimit = $this->getParam('iLimit') ? $this->getParam('iLimit') : Phpfox::getParam('feed.feed_display_limit');
        $sViewId = $this->getParam('sViewId') ? $this->getParam('sViewId') : 'all';
        
		$iUserId = intval($this->getParam('user_id'));

		$bIsCustomFeedView = false;
		$sCustomViewType = null;		
		
		if (PHPFOX_IS_AJAX && ($iUserId = $this->request()->get('profile_user_id')))
		{
			if (!defined('PHPFOX_IS_USER_PROFILE'))
			{
				define('PHPFOX_IS_USER_PROFILE', true);
			}
			$aUser = Phpfox::getService('user')->get($iUserId);
			
			$this->template()->assign(array(
					'aUser' => $aUser
				)
			);
		}	
		
		if (PHPFOX_IS_AJAX && $this->request()->get('callback_module_id'))
		{
			$aCallback = Phpfox::callback($this->request()->get('callback_module_id') . '.getFeedDisplay', $this->request()->get('callback_item_id'));
			$this->setParam('aFeedCallback', $aCallback);
		}
		
		$aFeedCallback = $this->getParam('aFeedCallback', null);
		
		$bIsProfile = $iUserId > 0;
		
		if ($this->request()->get('feed') && $bIsProfile)
		{
			switch ($this->request()->get('flike'))
			{
				default:
					if ($sPlugin = Phpfox_Plugin::get('feed.component_block_display_process_flike'))
					{
						eval($sPlugin);
					}					
					break;
			}
		}
		
		if (defined('PHPFOX_IS_USER_PROFILE') && !Phpfox::getService('user.privacy')->hasAccess($iUserId, 'feed.view_wall'))
		{			
			return false;			
		}
		
		if (defined('PHPFOX_IS_PAGES_VIEW') && !Phpfox::getService('pages')->hasPerm(null, 'pages.share_updates'))
		{
			$aFeedCallback['disable_share'] = true;
		}		
		
		$iFeedPage = $this->request()->get('page', 0);
		
		if ($this->request()->getInt('status-id') 
			|| $this->request()->getInt('comment-id') 
			|| $this->request()->getInt('link-id')
			|| $this->request()->getInt('poke-id')
			|| $this->request()->getInt('feed')
		)
		{
			$bIsCustomFeedView = true;
			if ($this->request()->getInt('status-id'))
			{
				$sCustomViewType = Phpfox::getPhrase('wall.status_update') . ': #' . $this->request()->getInt('status-id');
			}
			elseif ($this->request()->getInt('link-id'))
			{
				$sCustomViewType = Phpfox::getPhrase('wall.link').': #' . $this->request()->getInt('link-id');
			}		
			elseif ($this->request()->getInt('poke-id'))
			{
				$sCustomViewType = Phpfox::getPhrase('wall.poke').': #' . $this->request()->getInt('poke-id');
			}			
			elseif ($this->request()->getInt('comment-id'))
			{
				$sCustomViewType = Phpfox::getPhrase('wall.wall_comment').': #' . $this->request()->getInt('comment-id');						
				
				Phpfox::getService('notification.process')->delete('feed_comment_profile', $this->request()->getInt('comment-id'), Phpfox::getUserId());
			}
			elseif ($this->request()->getInt('feed'))
			{
				$sCustomViewType = Phpfox::getPhrase('wall.feed');
			}
		}
		
		$iUserId = intval($this->getParam('user_id')); // fix an unknown error
		
		$aRows = Phpfox::getService('wall.feed')->callback($aFeedCallback)->get(($iUserId > 0 ? $iUserId : null), ($this->request()->get('feed') ? $this->request()->get('feed') : null), $iFeedPage, false, $sViewId);
		
		if (($this->request()->getInt('status-id') 
				|| $this->request()->getInt('comment-id') 
				|| $this->request()->getInt('link-id')
				|| $this->request()->getInt('poke-id')
			) 
			&& isset($aRows[0]))
		{
			$aRows[0]['feed_view_comment'] = true;
			$this->setParam('aFeed', array_merge(array('feed_display' => 'view', 'total_like' => $aRows[0]['feed_total_like']), $aRows[0]));	
		}	
		
		(($sPlugin = Phpfox_Plugin::get('feed.component_block_display_process')) ? eval($sPlugin) : false);		
		
		if ($bIsCustomFeedView && !count($aRows) && $bIsProfile)
		{
			$aUser = $this->getParam('aUser');
			
			$this->url()->send($aUser['user_name'], null, Phpfox::getPhrase('feed.the_activity_feed_you_are_looking_for_does_not_exist'));
		}
		
		$iUserid = ($bIsProfile > 0 ? $iUserId : null);
		$iTotalFeeds = (int) Phpfox::getComponentSetting(($iUserid === null ? Phpfox::getUserId() : $iUserid), 'feed.feed_display_limit_' . ($iUserid !== null ? 'profile' : 'dashboard'), $iLimit);

		if (!Phpfox::isMobile())
		{
			$this->template()->assign(array(
					'sHeader' => '' // must!
				)
			);
		}
        
        // Advanced Wall assign
        //$aLimits = array(5, 10, 15, 20, 25, 30);
        $aFeedTypes = array(
            "all" => Phpfox::getPhrase('wall.all_feeds'),
            "user_status" => Phpfox::getPhrase('wall.user_status')
        );
        $aFeedTypes["friend"] = Phpfox::getPhrase('wall.friends');
        if(Phpfox::isModule('photo'))
        {
            $aFeedTypes["photo"] = Phpfox::getPhrase('wall.photos');
        }
        if(Phpfox::isModule('link'))
        {
            $aFeedTypes["link"] = Phpfox::getPhrase('wall.links');
        }
        if(Phpfox::isModule('event'))
        {
            $aFeedTypes["event"] = Phpfox::getPhrase('wall.events');
        }
        if(Phpfox::isModule('fevent'))
        {
            $aFeedTypes["fevent"] = Phpfox::getPhrase('wall.events');
        }
        $aFeedTypes["feed_comment"] = Phpfox::getPhrase('wall.comments');
        if(Phpfox::isModule('marketplace'))
        {
            $aFeedTypes["marketplace"] = Phpfox::getPhrase('wall.marketplace');
        }
        if(Phpfox::isModule('marketplace'))
        {
            $aFeedTypes["marketplace"] = Phpfox::getPhrase('wall.marketplace');
        }
        if(Phpfox::isModule('poll'))
        {
            $aFeedTypes["poll"] = Phpfox::getPhrase('wall.polls');
        }
        if(Phpfox::isModule('blog'))
        {
            $aFeedTypes["blog"] = Phpfox::getPhrase('wall.blogs');
        }
        if(Phpfox::isModule('video'))
        {
            $aFeedTypes["video"] = Phpfox::getPhrase('wall.videos');
        }
        if(Phpfox::isModule('music'))
        {
            $aFeedTypes["music_song"] = Phpfox::getPhrase('wall.music_songs');
        }
        if(Phpfox::hasCallback('document', 'getNewsFeed'))
        {
            $aFeedTypes["document"] = Phpfox::getPhrase('wall.documents');
        }
        if(Phpfox::hasCallback('musicstore', 'getNewsFeed'))
        {
            $aFeedTypes["musicstore_album"] = Phpfox::getPhrase('wall.musicstore_albums');
            $aFeedTypes["musicstore_playlist"] = Phpfox::getPhrase('wall.musicstore_playlists');
        }
        if(Phpfox::hasCallback('musicsharing', 'getNewsFeed'))
        {
            $aFeedTypes["musicsharing_album"] = Phpfox::getPhrase('wall.musicsharing_albums');
            $aFeedTypes["musicsharing_playlist"] = Phpfox::getPhrase('wall.musicsharing_playlists');
        }
        $this->template()->assign(array(
            "aFeedTypes" => $aFeedTypes/*,
            "aLimits" => $aLimits*/
        ));
        $iCurUserId = Phpfox::getUserId();
        $sView = $bIsProfile ? "profile" : "index";
        $aUser = $this->getParam('aUser');
        $iWallOwnerId = !empty($aUser['user_id']) ? $aUser['user_id'] : $iCurUserId;
        $this->template()->assign(array(
            'sCorePath' => Phpfox::getParam('core.path'),
            'sImageWall' => Phpfox::getParam('core.path') . 'module/wall/static/image',
            'sView' => $sView,
            'iWallOwnerId' => $iWallOwnerId,
            'iCurUserId' => $iCurUserId
        ));
        
        // Emoticons list for js
        $oEmoticon = Phpfox::getService('emoticon');
        $aPackages = $oEmoticon->getPackages();
        $aEmoticons = array();
        foreach($aPackages as $aPackage)
        {
            if($aPackage["is_active"]==1)
            {
                $aEmoticons = array_merge($aEmoticons, $oEmoticon->getEmoticons($aPackage["package_path"]));
            }
        }
        $this->template()->assign(array(
            "aEmoticons" => $aEmoticons
        ));
        
        // Friends list for js
        $aFriends = Phpfox::getService('friend')->get(array(), 'friend.time_stamp DESC', '', '', $bCount = false, true, false, $iCurUserId, false);
        $this->template()->assign(array(
            "aFriends" => $aFriends
        ));
        
        // Check feeds visibility
        $aRows = Phpfox::getService('wall.process')->fillVisibility($aRows, $iCurUserId, $sView, $iWallOwnerId);
        $aRows = Phpfox::getService('wall.process')->displayLinks($aRows);
		$this->template()->assign(array(
				'iUserId' => $iUserId,
				'aFeeds' => $aRows,
				'iFeedNextPage' => ($iFeedPage + 1),
				'iFeedCurrentPage' => $iFeedPage,
				'iTotalFeedPages' => 1,
				'aFeedVals' => $this->request()->getArray('val'),
				'sCustomViewType' => $sCustomViewType,
				'aFeedStatusLinks' => Phpfox::getService('wall.feed')->getShareLinks(),
				'aFeedCallback' => $aFeedCallback,
				'bIsCustomFeedView' => $bIsCustomFeedView,
				'phrase_feed_what_s_on_your_mind' => Phpfox::getPhrase('feed.what_s_on_your_mind'),
				'phrase_feed_write_something' => Phpfox::getPhrase('feed.write_something'),
				//'phrase_music_say_something_about_this_song' => Phpfox::getPhrase('music.say_something_about_this_song'),
                                'pharse_photo_say_something_about_this_photo' => Phpfox::getPhrase('photo.say_something_about_this_photo')
			)
		);
        
		if ($bIsProfile)
		{			
			if (!Phpfox::getService('user.privacy')->hasAccess($iUserId, 'feed.display_on_profile'))
			{
				return false;
			}			
		}
				
		return 'block';
	}

	public function clean()
	{
		$this->template()->clean(array(
				'sHeader',
				'aFeeds',
				'sBoxJsId'
			)
		);
	}
}

?>